﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Ring2Park.Models;

namespace Ring2Park.Controllers
{ 
    public class ParkingLocationController : Controller
    {
        private Ring2ParkEntities db = new Ring2ParkEntities();

        //
        // GET: /ParkingLocation/

        public ViewResult Index()
        {
            return View(db.ParkingLocations.ToList());
        }

        //
        // POST: /ParkingLocation/id/5

        [HttpPost]  
        public ActionResult Index(int? parkingLocationId) {
            if (!parkingLocationId.HasValue)
            {
                return View();
            }
            else
            {
                ParkingLocation parkingLocation = db.ParkingLocations.Find(parkingLocationId);
                return View(parkingLocation);
            }  
        }  


        //
        // GET: /ParkingLocation/Details/5

        public ViewResult Details(int id)
        {
            ParkingLocation parkinglocation = db.ParkingLocations.Find(id);
            return View(parkinglocation);
        }

        //
        // GET: /ParkingLocation/Create

        [Authorize(Roles = "Administrator")]
        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /ParkingLocation/Create

        [Authorize(Roles = "Administrator")]
        [HttpPost]
        public ActionResult Create(ParkingLocation parkinglocation)
        {
            if (ModelState.IsValid)
            {
                db.ParkingLocations.Add(parkinglocation);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            return View(parkinglocation);
        }
        
        //
        // GET: /ParkingLocation/Edit/5

        [Authorize(Roles = "Administrator")]
        public ActionResult Edit(int id)
        {
            ParkingLocation parkinglocation = db.ParkingLocations.Find(id);
            return View(parkinglocation);
        }

        //
        // POST: /ParkingLocation/Edit/5

        [Authorize(Roles = "Administrator")]
        [HttpPost]
        public ActionResult Edit(ParkingLocation parkinglocation)
        {
            if (ModelState.IsValid)
            {
                db.Entry(parkinglocation).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(parkinglocation);
        }

        //
        // GET: /ParkingLocation/Delete/5

        [Authorize(Roles = "Administrator")]
        public ActionResult Delete(int id)
        {
            ParkingLocation parkinglocation = db.ParkingLocations.Find(id);
            return View(parkinglocation);
        }

        //
        // POST: /ParkingLocation/Delete/5

        [Authorize(Roles = "Administrator")]
        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            ParkingLocation parkinglocation = db.ParkingLocations.Find(id);
            db.ParkingLocations.Remove(parkinglocation);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        [AllowAnonymous]
        public JsonResult List(string term) {  
            var results = from p in db.ParkingLocations  
                   where p.Location.Contains(term)  
                   select new { label = p.Name, id = p.ParkingLocationId };  
            
           return Json(results.ToArray(), JsonRequestBehavior.AllowGet);  
        }  

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}